package com.xxx.service;

import com.xxx.common.Result;
import com.xxx.eneity.Team;
import com.xxx.eneity.User;
import com.xxx.eneity.noticeStatus;
import com.xxx.mapper.TeamMapper;
import com.xxx.mapper.UserMapper;
import com.xxx.out.TeamInfo;
import com.xxx.out.TeamMember;
import com.xxx.out.TeamNotice;
import com.xxx.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeamServiceImpl implements TeamService{

    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int removeTeam(Integer teamId) {
        return teamMapper.removeTeam(teamId);
    }

    @Override
    public int replaceCaptain(Integer teamId, Integer userId) {
        teamMapper.deleteCaptain(teamId);
        int i = teamMapper.updateCaptain(teamId, userId);
        return i;
    }

    @Override
    public int kickOutMember(Integer userId, Integer teamId) {
        return teamMapper.kickOutMember(userId, teamId);
    }

    @Override
    public int addMember(Integer userId, Integer teamId) {
        String username = userMapper.getUserById(userId).getName();
        return teamMapper.addMember(userId, teamId, username);
    }

    @Override
    public List<UserVo> getOtherUserList(Integer teamId) {
        // 获取所有用户列表
        List<UserVo> userList = userMapper.getUserList();
        Integer contestId = teamMapper.getContestId(teamId);
        // 获取该赛事队伍队员和队长列表
        List<UserVo> teamMemberList = teamMapper.getTeamMemberList(contestId);
        // 去除
        userList.removeAll(teamMemberList);
        return userList;
    }

    @Override
    public Boolean isCaptain(Integer teamId, Integer userId) {
        int count = teamMapper.isCaptain(teamId, userId);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int editTeam(EditTeamVo editTeamVo) {
        return teamMapper.editTeam(editTeamVo);
    }

    @Override
    public List<noticeStatus> getReadStatus(Integer noticeId) {
        Integer teamId = teamMapper.getTeamId(noticeId);
        return teamMapper.getReadStatus(noticeId, teamId);
    }

    @Override
    public int isReadTeamNotice(Integer userId, Integer noticeId) {
        return teamMapper.isReadTeamNotice(userId, noticeId);
    }

    @Override
    public int readTeamNotice(Integer userId, Integer noticeId) {
        return teamMapper.readTeamNotice(userId, noticeId);
    }

    @Override
    public Integer exitTeam(Integer teamId, Integer userId) {
        return teamMapper.exitTeam(teamId, userId);
    }

    @Override
    public List<TeamInfo> getTeamListAsCaptain(Integer userId) {
        return teamMapper.getTeamListAsCaptain(userId);
    }

    @Override
    public TeamInfo getTeamName(Integer contestId, Integer userId) {
        return teamMapper.getTeamName(contestId, userId);
    }

    @Override
    public boolean isMemberExistContest(Integer contestId, Integer userId) {
        int result = teamMapper.isMemberExist(contestId, userId);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Result reviewApplication(boolean advice, Integer teamId, Integer userId) {
        if (advice){
            teamMapper.reviewApplication(1,teamId,userId);
            Result<Object> objectResult = new Result<>();
            objectResult.setCode(200);
            objectResult.setMsg("已成功将队员加入该队伍");
            return objectResult;
        }else {
            teamMapper.reviewApplication(2,teamId,userId);
            Result<Object> objectResult = new Result<>();
            objectResult.setCode(-100);
            objectResult.setMsg("申请已被驳回");
            return objectResult;
        }
    }

    @Override
    public List<TeamInfo> getTeamInfoList(Integer contestId) {
        return teamMapper.getTeamInfoList(contestId);
    }

    @Override
    public List<TeamNotice> getTeamNoticeList(Integer teamId) {
        return teamMapper.getTeamNoticeList(teamId);
    }

    @Override
    public List<TeamNotice> getTeamNoticeStatusList(Integer teamId, Integer userId) {
        return teamMapper.getTeamNoticeStatusList(teamId, userId);
    }

    @Override
    public int removeTeamNotice(Integer noticeId) {
        return teamMapper.removeTeamNotice(noticeId);
    }

    @Override
    public int editTeamNotice(EditNoticeVo editNoticeVo) {
        return teamMapper.editTeamNotice(editNoticeVo);
    }

    @Override
    public int addTeamNotice(TeamNoticeVo teamNoticeVo) {
        return teamMapper.addTeamNotice(teamNoticeVo);
    }

    @Override
    public int reviewJoinRequest(Integer userId, Boolean advice, Integer teamId) {
        return teamMapper.reviewJoinRequest(userId, advice, teamId);
    }

    @Override
    public List<TeamMember> getJoinMember(Integer teamId) {
        return teamMapper.getJoinMembers(teamId);
    }

    @Override
    public List<TeamMember> getTeamMembers(Integer teamId) {
        return teamMapper.getTeamMembers(teamId);
    }

    @Override
    public TeamInfo getTeamInfo(Integer teamId) {
        return teamMapper.getTeamInfo(teamId);
    }

    @Override
    public boolean isMemberExist(Integer teamId, Integer userId) {
        Integer contestId = teamMapper.getContestId(teamId);
        int result = teamMapper.isMemberExist(contestId, userId);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int addTeamMember(JoinTeamVo joinTeamVo) {
        // 根据userId获取username
        User user = userMapper.getUserById(joinTeamVo.getUserId());
        joinTeamVo.setUserName(user.getName());
        return teamMapper.addTeamMember(joinTeamVo);
    }

    @Override
    public List<TeamInfo> getTeamList(Integer userId) {
        return teamMapper.getTeamList(userId);
    }

    @Override
    public Boolean isTeamNameExist(String name, Integer contestId) {
        int count = teamMapper.isTeamNameExist(name, contestId);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean isTeamNameExist(Integer teamId, String teamName) {
        //  获取赛事编号
        Integer contestId = teamMapper.getContestId(teamId);
        int count = teamMapper.isTeamNameExistX(teamName, contestId, teamId);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Team createTeam(CreateTeamVo createTeamVo) {
        teamMapper.createTeam(createTeamVo);
        Integer teamId = createTeamVo.getId();
        User user = userMapper.getUserById(createTeamVo.getUserId());
        teamMapper.addCaptain(teamId, createTeamVo.getUserId(), user.getName());
        Team team = teamMapper.getTeamById(teamId);
        return team;
    }
}
